// 用于测试API接口是否可用的脚本
import axios from 'axios';
import { API_BASE_URL } from './api/config';

// 创建axios实例
const apiClient = axios.create({
  baseURL: API_BASE_URL,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 测试获取活动统计数据
const testGetUserActivityStats = async () => {
  try {
    const params = {
      userId: '', // 空字符串获取所有用户的数据
      startDate: '2023-10-01',
      endDate: new Date().toISOString()
    };
    
    // 构建URL查询参数
    const queryString = Object.entries(params)
      .filter(([_, value]) => value !== null && value !== undefined)
      .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
      .join('&');
    
    console.log(`测试API: ${API_BASE_URL}/activities/stats?${queryString}`);
    const response = await apiClient.get(`/activities/stats?${queryString}`);
    console.log('获取活动统计数据成功:', response.data);
    return response.data;
  } catch (error) {
    console.error('获取活动统计数据失败:', error);
    console.log('错误详情:', error.response ? error.response.data : error.message);
    throw error;
  }
};

// 测试获取训练强度分布
const testGetTrainingIntensity = async () => {
  try {
    const params = {
      userId: '', // 空字符串获取所有用户的数据
      startDate: '2023-10-01',
      endDate: new Date().toISOString()
    };
    
    // 构建URL查询参数
    const queryString = Object.entries(params)
      .filter(([_, value]) => value !== null && value !== undefined)
      .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
      .join('&');
    
    console.log(`测试API: ${API_BASE_URL}/activities/intensity?${queryString}`);
    const response = await apiClient.get(`/activities/intensity?${queryString}`);
    console.log('获取训练强度分布成功:', response.data);
    return response.data;
  } catch (error) {
    console.error('获取训练强度分布失败:', error);
    console.log('错误详情:', error.response ? error.response.data : error.message);
    throw error;
  }
};

// 执行测试
const runTests = async () => {
  console.log('开始API测试...');
  try {
    await testGetUserActivityStats();
    await testGetTrainingIntensity();
    console.log('所有API测试通过!');
  } catch (error) {
    console.error('API测试失败!', error);
  }
};

// 导出测试函数
export { testGetUserActivityStats, testGetTrainingIntensity, runTests };

// 如果直接运行此脚本，则执行测试
if (typeof require !== 'undefined' && require.main === module) {
  runTests();
} 